/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.setup.review;

import com.nus.application.ejb.reviewtemplate.ReviewTemplateEjbLocal;
import com.nus.application.web.base.BaseAction;
import com.nus.application.web.setup.review.form.ReviewTemplateForm;
import com.nus.dataaccess.dao.reviewtemplate.ReviewTemplate;
import com.nus.dataaccess.dao.reviewtemplate.TemplateSection;
import com.opensymphony.xwork2.ValidationAware;
import javax.inject.Inject;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author robin.foe
 */
public class ReviewTemplateMaintenance extends BaseAction implements ValidationAware{
    
    @Inject
    ReviewTemplateEjbLocal reviewTemplateEjb;
    
    public String doList(){
        System.err.println(reviewTemplateEjb.test("hello"));
        this.form.setFullListSize(reviewTemplateEjb.countTotal(null));
        this.form.setPageNumber(super.getPage());
        this.getForm().setReviewTemplates(
                reviewTemplateEjb.paginate(form.calculateStartRow(super.getPage()), form.getObjectsPerPage(), null, false, null)
                );
        
        return SUCCESS;
    }
    
    public String doCreate(){
//        this.form.setReviewTemplate(new ReviewTemplate());
        
        ReviewTemplate reviewTemplate = new ReviewTemplate();//this.getForm().getReviewTemplate();
        reviewTemplate.getTemplateSection().add(new TemplateSection());
        this.getForm().setReviewTemplate(reviewTemplate);
        
        return SUCCESS;
    }
    
    public String doSave(){
        reviewTemplateEjb.persist(this.form.getReviewTemplate());
        
        return SUCCESS;
    }
    
    public String doAddSection(){
        System.err.println("hello");
        ReviewTemplate reviewTemplate = this.getForm().getReviewTemplate();
        reviewTemplate.getTemplateSection().add(new TemplateSection());
        this.getForm().setReviewTemplate(reviewTemplate);
        System.err.println(" " + reviewTemplate.getTemplateSection().size());
        return SUCCESS;
    }
    
    public String doRemoveSection(){
        ReviewTemplate reviewTemplate = this.getForm().getReviewTemplate();
        reviewTemplate.getTemplateSection().remove(this.getForm().getSelectedIndex());
        
        
        return SUCCESS;
    }
    
    private ReviewTemplateForm form = new ReviewTemplateForm();
    public ReviewTemplateForm getForm() {return form;}
    public void setForm(ReviewTemplateForm form) {this.form = form;}
    
    @Override
    public void validate(){
        
        if(!super.isSaveEvent()){
            if(super.isListEvent()){
                super.clearActionErrors();
            }
            return;
        }
        
        if(StringUtils.isEmpty(this.getForm().getReviewTemplate().getCode())){
            super.addActionError(super.getText("error.code_is_required"));
        }
        
        if(StringUtils.isEmpty(this.getForm().getReviewTemplate().getDescription())){
            super.addActionError(super.getText("error.description_is_required"));
        }
        
    }
    
}
